package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-21
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {
    /**
     * 保存在媒体用户
     * @param wmUser
     * @return
     */
    @Override
    public ResponseResult<WmUser> saveUser(WmUser wmUser) {
//        效验参数是否有效
        if (wmUser == null || wmUser.getApUserId() == null || StringUtils.isEmpty(wmUser.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        添加
//        查询并判断是否已经存在
        LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
        query.eq(WmUser::getApUserId,wmUser.getApUserId());
        WmUser one = this.getOne(query);
        if (one != null){
//            存在 返回数据
            return ResponseResult.okResult(one);
        }
//        不存在添加数据
        wmUser.setCreatedTime(new Date());
        this.save(wmUser);

        return ResponseResult.okResult(wmUser);
    }

    @Override
    public ResponseResult login(LoginDto dto)  {
//        验证dto及参数是否为空
        if (dto == null || StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        查询用户名是否存在
        LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
        query.eq(WmUser::getName,dto.getName());
        WmUser one = this.getOne(query);
        if (one == null){
//            用户不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        用户存在
//        获取盐
        String salt = one.getSalt();
//        密码加盐
        String password = dto.getPassword()+salt;
//        md5加密
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
//        判断密码是否正确
        if (one.getPassword().equals(s)){
//            密码正确
//            生成token
            Map<String,Object> body = new HashMap<>();
            body.put("userId",one.getId());
            body.put("username",one.getName());
            String token = AppJwtUtil.getToken(body);
            Map<String,Object> map = new HashMap<>();
            one.setPhone(null);
            one.setPassword(null);
            one.setSalt(null);
            map.put("user",one);
            map.put("token",token);
            return ResponseResult.okResult(map);
        }
//        密码不正确
        return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
    }
}
